package com.bluegalaxy.sitescore.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.plugins.Page;
import com.bluegalaxy.sitescore.entity.*;
import com.bluegalaxy.sitescore.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LZRuil on 2017/12/25.
 */
@Controller
@RequestMapping("/surfaceWater")
public class SurfaceWaterController extends BaseController {

    @Autowired
    private SurfaceWaterGWService surfaceWaterGWService;

    @Autowired
    private SurfaceWaterService surfaceWaterService;

    @Autowired
    private ITBSelectDangerousService itbSelectDangerousService;

    @Autowired
    private ITbDangerousService tbDangerousService;

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/add/{siteId}/{id}")
    public String list(Model model,@PathVariable  int id,@PathVariable int siteId) {
        int sourceId = siteId;

        if(id!=0){
            SurfaceWaterOL surfaceWaterOL = surfaceWaterService.getSurfaceWaterById(id);
            if(surfaceWaterOL == null){
                id=0;
            }
            model.addAttribute("surfaceWater",surfaceWaterOL);
        }

        //model.addAttribute("sWList",toJson(surfaceWaterService.getSWOverLandList()));
        List<SelectDangerous> selectDangerousList = itbSelectDangerousService.getDangerousBysid(sourceId,3);
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("sdList",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(sourceId);
            model.addAttribute("sdList",toJson(dangerousList));
        }

        //    System.out.println(selectDangerousList.get(0).getChemname());
        List<TbScore> tbScoreList = surfaceWaterService.getSWOverLandList(sourceId);
        model.addAttribute("sWList",toJson(tbScoreList));
//        model.addAttribute("sdList",toJson(selectDangerousList));
        model.addAttribute("sIndex",0);
        model.addAttribute("sId",id);
        return "/surfaceWater/add";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/add2/{siteId}/{id}")
    public String list2(Model model,@PathVariable  int siteId,@PathVariable  int id) {
        int sourceId = siteId ;
        if(id!=0){
            SurfaceWaterGW surfaceWaterGW = surfaceWaterGWService.getGroundSurfaceById(id);
            if(surfaceWaterGW == null){
                id = 0;
            }
            model.addAttribute("surfaceWaterGW",surfaceWaterGW);
        }
        List<SelectDangerous> selectDangerousList = itbSelectDangerousService.getDangerousBysid(sourceId,4);
//        model.addAttribute("sdList",toJson(selectDangerousList));
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("sdList",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(sourceId);
            model.addAttribute("sdList",toJson(dangerousList));
        }
        List<TbScore> tbScoreList =surfaceWaterGWService.getSWGroundList(sourceId);
        model.addAttribute("sIndex",1);
        model.addAttribute("gId",id);
        model.addAttribute("gWList",toJson(tbScoreList));
        return "/surfaceWater/add2";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/index/{siteId}/{sid}/{gid}/{sIndex}")
    public String index(Model model,@PathVariable int siteId,@PathVariable  int sid,@PathVariable  int gid,@PathVariable  int sIndex) {
       /* if(id!=0&sIndex==0){
            SurfaceWaterOL surfaceWaterOL = surfaceWaterService.getSurfaceWaterById(id);
            model.addAttribute("surfaceWater",surfaceWaterOL);
        }*/
        List<TbScore> sWList = surfaceWaterService.getSWOverLandList(siteId);
        List<TbScore> gWList = surfaceWaterGWService.getSWGroundList(siteId);
        if(surfaceWaterService.getSurfaceWaterById(sid)==null){
            sid = 0;
        }
        if(surfaceWaterGWService.getGroundSurfaceById(gid)==null){
            gid = 0;
        }
        model.addAttribute("sWList",toJson(sWList));
        model.addAttribute("gWList",toJson(gWList));
        model.addAttribute("sIndex",sIndex);
        model.addAttribute("sId",sid);
        model.addAttribute("gId",gid);
        model.addAttribute("siteId",siteId);
        return "/surfaceWater/index";
    }

   /* @ResponseBody
    @Permission("2001")
    @RequestMapping("/getSWList")
    public String getUserList() {
        Page<TbSurfaceWaterOverLand> page = getPage();
        return jsonPage(surfaceWaterService.selectPage(page,null));
    }*/

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/surface{id}")
    public String surface(Model model, @PathVariable String id) {
        return "/surfaceWater/surface"+id;
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/editScore/{siteId}/{id}",method = RequestMethod.GET)
    public @ResponseBody
    String OverLandEdit(@PathVariable String id,@PathVariable int siteId ,Model model){
        SurfaceWaterOL surfaceWaterOL = surfaceWaterService.getSurfaceWaterById(Integer.parseInt(id));
        model.addAttribute("surfaceWater",surfaceWaterOL);
        model.addAttribute("sIndex",1);
        return "/surfaceWater/add";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/delSurfaceScore",method = RequestMethod.POST)
    public @ResponseBody
    String OverLandDel(int siteId,int id, Model model){
        int sourceId = siteId;
        //  System.out.println(id+"---------------");
        if (surfaceWaterService.delScore(id,sourceId)){
            model.addAttribute("sIndex",1);
            return "{\"success\":\"删除成功\"}";
        }else {
            model.addAttribute("sIndex",1);
            return "{\"false\":\"删除异常\"}";
        }

    }


    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/delGroudScore",method = RequestMethod.POST)
    public @ResponseBody
    String GroundDel(int siteId,int id, Model model){
        int sourceId = siteId;
        System.out.println(id+"---------------");
        if (surfaceWaterGWService.delScore(id,sourceId)){
            model.addAttribute("sIndex",1);
            return "{\"success\":\"删除成功\"}";
        }else {
            model.addAttribute("sIndex",1);
            return "{\"false\":\"删除异常\"}";
        }

    }



    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/caculateScore/{siteId}",method = RequestMethod.POST)
    public @ResponseBody
    String OverLandCaculate(SurfaceWaterOL surfaceWaterOL, Model model,@PathVariable int siteId){
        int sourceId = siteId;
        //2、使用JSONArray
        JSONArray jsonArray= JSONArray.parseArray(surfaceWaterOL.getSdList());
        List<SelectDangerous> selectDangerousList = new ArrayList<>();
        List<Integer> selectDangerousIdList = new ArrayList<>();
        //获得jsonArray的第一个元素
        for (int i =0;i<jsonArray.size();i++){
            Object o=jsonArray.get(i);
            SelectDangerous selectDangerous=(SelectDangerous)JSONObject.parseObject(JSONObject.toJSONString(o), SelectDangerous.class);
            if(selectDangerous.getDangerous_id()==0){
                selectDangerous.setDangerous_id(selectDangerous.getId());
            }else{
                selectDangerous.setDangerous_id(selectDangerous.getDangerous_id());
            }
            selectDangerous.setType(3);
            selectDangerous.setSource_id(sourceId);
            selectDangerousList.add(selectDangerous);
            selectDangerousIdList.add(selectDangerous.getId());
        }
        if( itbSelectDangerousService.getDangerousBysid(sourceId,3).isEmpty()){
            if (!itbSelectDangerousService.insertDangerousList2(selectDangerousList)){
                return "false";
            }
        }else{
            if (itbSelectDangerousService.delDangerousListById(selectDangerousIdList)){
                if (!itbSelectDangerousService.insertDangerousList(selectDangerousList)){
                    return "false";
                }
            }else {
                return "false";
            }
        }
        /*if (itbSelectDangerousService.delDangerousListById(selectDangerousIdList)){
            if (!itbSelectDangerousService.insertDangerousList(selectDangerousList)){
                return "false";
            }
        }else {
            return "false";
        }*/
        if (surfaceWaterService.judgeScore(surfaceWaterOL)){
            BigDecimal release = surfaceWaterService.caculateScore(surfaceWaterOL,sourceId);
            return   "{\"success\":\"保存成功\"}";
            //   System.out.println(release);
        }else {
            return "false";
        }

    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/caculateScoreGroud/{siteId}",method = RequestMethod.POST)
    public @ResponseBody
    String GroudWaterCaculate(SurfaceWaterGW surfaceWaterOL, Model model,@PathVariable int siteId){
        int sourceId = siteId;
        //System.out.println(surfaceWaterOL);
       JSONArray jsonArray= JSONArray.parseArray(surfaceWaterOL.getSdList());
        List<SelectDangerous> selectDangerousList = new ArrayList<>();
        List<Integer> selectDangerousIdList = new ArrayList<>();
        //获得jsonArray的第一个元素
        for (int i =0;i<jsonArray.size();i++){
            Object o=jsonArray.get(i);
            SelectDangerous selectDangerous=(SelectDangerous)JSONObject.parseObject(JSONObject.toJSONString(o), SelectDangerous.class);
            if(selectDangerous.getDangerous_id()==0){
                selectDangerous.setDangerous_id(selectDangerous.getId());
            }else{
                selectDangerous.setDangerous_id(selectDangerous.getDangerous_id());
            }
            selectDangerous.setType(4);
            selectDangerous.setSource_id(sourceId);
            selectDangerousList.add(selectDangerous);
            selectDangerousIdList.add(selectDangerous.getId());
        }
       /* if (itbSelectDangerousService.delDangerousListById(selectDangerousIdList)){
            if (!itbSelectDangerousService.insertDangerousList(selectDangerousList)){
                return "false";
            }
        }else {
            return "false";
        }*/
        if( itbSelectDangerousService.getDangerousBysid(sourceId,4).isEmpty()){
            if (!itbSelectDangerousService.insertDangerousList2(selectDangerousList)){
                return "false";
            }
        }else{
            if (itbSelectDangerousService.delDangerousListById(selectDangerousIdList)){
                if (!itbSelectDangerousService.insertDangerousList(selectDangerousList)){
                    return "false";
                }
            }else {
                return "false";
            }
        }
        if (surfaceWaterGWService.judgeScoreGroud(surfaceWaterOL)){
            BigDecimal release = surfaceWaterGWService.caculateScoreGroud(surfaceWaterOL,sourceId);
        //    System.out.println(release);
        }else {
            return "false";
        }
        return  "success";
    }
}
